<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\BaseController;
use App\Models\Cache\Cache;
use App\Models\Cache\CacheKey;
use App\Models\SysArticle;
use Illuminate\Http\Request;

class RedirectController extends BaseController
{
    public function article(Request $request)
    {
        $refer = "http://www.qq.com/";

        $params = $request->all();

        $cacheData = Cache::getInstance([
            'url' => SysArticle::getInstance($params['aid'])->where('article_id', $params['aid'])->value('original_url')
        ])->hGetAll(CacheKey::ONE_CLICK_COLLECT_REDIRECT_MAP);

        $url = $cacheData[$params['sign']] ?? '';
        $nginxRedirectUrl = $url;
        $opt = [
            'http' => [
                'header' => "Referer: " . $refer
            ]
        ];

        $context = stream_context_create($opt);
        $file_contents = file_get_contents($nginxRedirectUrl, false, $context);
        echo $file_contents;

        return $this->apiReturn(true);
    }
}
